package org.codeart.apipassenger.controller;

import org.codeart.apipassenger.service.VerificationCodeService;
import org.codeart.common.util.R;
import org.codeart.common.dto.VerifyCodeDTO;
import org.codeart.common.response.TokenResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class VerificationCodeController {

    @Autowired
    private VerificationCodeService verificationCodeService;

    @PostMapping("/verification-code")
    public R<Void> sendCode(@RequestBody VerifyCodeDTO verifyCodeDTO) {
        String passengerPhone = verifyCodeDTO.getPassengerPhone();
        boolean b = verificationCodeService.generateCode(passengerPhone);
        if (b) {
            return R.ok();
        }
        return R.fail();
    }

    @PostMapping("/verify-code-check")
    public R<TokenResponse> checkVerificationCode(@RequestBody VerifyCodeDTO verifyCodeDTO) {
        String phone = verifyCodeDTO.getPassengerPhone();
        String code = verifyCodeDTO.getVerificationCode();
        TokenResponse tokenResponse = verificationCodeService.checkCode(phone, code);
        if (tokenResponse == null) {
            return R.fail("验证码错误");
        }
        return R.ok(tokenResponse);
    }

}
